package servlet;
import dao.MetaverseProductMapper;
import entity.MetaverseProduct;
import org.apache.ibatis.session.SqlSession;
import utils.MyBatisUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = "/metaverseProductListServlet")
public class metaverseProductListServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        List<MetaverseProduct> list = new ArrayList<>();
        SqlSession sqlSession = null;
        String name=request.getParameter("name");
        Integer flag=Integer.parseInt(request.getParameter("flag")!=null?request.getParameter("flag"):"2");
        Integer pageIndex=Integer.parseInt(request.getParameter("pageIndex"));
        Integer pageSize=4;
        Integer pageBegin=(pageIndex-1)*pageSize;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            list = sqlSession.getMapper(MetaverseProductMapper.class).selectall(name,flag,pageBegin,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        request.setAttribute("list",list);
        request.setAttribute("name",name);
        request.setAttribute("flag",flag);
        request.setAttribute("pageIndex",pageIndex);
        request.getRequestDispatcher("metaverseProductList.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
